package com.fireline.beauty.cashier;

import java.math.BigDecimal;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import com.zgw.fireline.base.widgets.ControlUtil;

public class QkfkDialog extends Dialog {

	protected int result;
	protected Shell shell;
	private Text text;
	private Label labQkje;
	private String xfje;
	private Button btnBfqk;

	public String qkje="0";
	public String xj = "0";
	public String xyk = "0";
	public String czk = "0";
	public String djq = "0";
	private Button btnXj;
	private Button btnCzk;
	private Button btnXyk;
	private Button btnDjq;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public QkfkDialog(Shell parent, String xfje) {
		super(parent, SWT.NONE);
		this.xfje = xfje;
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public int open() {
		createContents();
		ControlUtil.centerShell(shell);
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM);
		shell.setSize(399, 316);
		shell.setText("欠款");
		GridLayout gl_shell = new GridLayout(3, false);
		gl_shell.verticalSpacing = 15;
		gl_shell.marginLeft = 10;
		gl_shell.marginWidth = 10;
		gl_shell.marginHeight = 20;
		shell.setLayout(gl_shell);

		Label lblNewLabel = new Label(shell, SWT.NONE);
		lblNewLabel.setFont(SWTResourceManager.getFont("宋体", 14, SWT.BOLD));
		lblNewLabel.setText("本次消费");

		Label labBcxf = new Label(shell, SWT.NONE);
		GridData gd_labBcxf = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				1, 1);
		gd_labBcxf.widthHint = 70;
		labBcxf.setLayoutData(gd_labBcxf);
		labBcxf.setFont(SWTResourceManager.getFont("宋体", 14, SWT.BOLD));
		labBcxf.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		labBcxf.setText(xfje);
		Label label = new Label(shell, SWT.NONE);
		label.setFont(SWTResourceManager.getFont("宋体", 14, SWT.BOLD));
		label.setText("元");

		Button btnQbqk = new Button(shell, SWT.RADIO);
		btnQbqk.setSelection(true);
		GridData gd_btnQbqk = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				2, 1);
		gd_btnQbqk.verticalIndent = 5;
		btnQbqk.setLayoutData(gd_btnQbqk);
		btnQbqk.setText("全部欠款");
		new Label(shell, SWT.NONE);

		btnBfqk = new Button(shell, SWT.RADIO);
		btnBfqk.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false,
				2, 1));
		btnBfqk.setText("部分欠款");
		new Label(shell, SWT.NONE);

		final Composite composite_2 = new Composite(shell, SWT.BORDER);
		composite_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 3, 1));
		GridLayout gl_composite_2 = new GridLayout(5, false);
		gl_composite_2.marginHeight = 15;
		gl_composite_2.verticalSpacing = 20;
		composite_2.setLayout(gl_composite_2);

		Label label_1 = new Label(composite_2, SWT.NONE);
		label_1.setFont(SWTResourceManager.getFont("宋体", 14, SWT.BOLD));
		label_1.setText("付款金额：");

		text = new Text(composite_2, SWT.BORDER);
		text.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		text.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				countQkje();
			}
		});
		text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		text.setFont(SWTResourceManager.getFont("宋体", 14, SWT.BOLD));
		ControlUtil.setCurrencyRule(text, false, false);
		ControlUtil.setEnterToFocusMove(text);

		Label lblNewLabel_2 = new Label(composite_2, SWT.NONE);
		GridData gd_lblNewLabel_2 = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_lblNewLabel_2.horizontalIndent = 5;
		lblNewLabel_2.setLayoutData(gd_lblNewLabel_2);
		lblNewLabel_2.setText("欠款金额：");

		labQkje = new Label(composite_2, SWT.NONE);
		labQkje.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		labQkje.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		labQkje.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		labQkje.setText("0");

		Label label_2 = new Label(composite_2, SWT.NONE);
		label_2.setText("元");

		final Composite composite = new Composite(composite_2, SWT.NONE);
		composite.setLayout(new GridLayout(4, false));
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 5, 1));

		btnXj = new Button(composite, SWT.RADIO);
		btnXj.setSelection(true);
		btnXj.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1,
				1));
		btnXj.setText("现金");

		btnCzk = new Button(composite, SWT.RADIO);
		btnCzk.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1,
				1));
		btnCzk.setText("储值卡");

		btnXyk = new Button(composite, SWT.RADIO);
		btnXyk.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1,
				1));
		btnXyk.setText("信用卡");

		btnDjq = new Button(composite, SWT.RADIO);
		btnDjq.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1,
				1));
		btnDjq.setText("代金券");

		final Composite composite_1 = new Composite(shell, SWT.NONE);
		composite_1.setLayout(new GridLayout(2, false));
		composite_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 3, 1));

		Button btnNewButton = new Button(composite_1, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doOkPressed();
			}
		});

		Listener enableListener = new Listener() {
			@Override
			public void handleEvent(Event event) {
				for (Control c : composite_2.getChildren()) {
					c.setEnabled(btnBfqk.getSelection());
				}
				for (Control c : composite.getChildren()) {
					c.setEnabled(btnBfqk.getSelection());
				}
			}
		};
		btnBfqk.addListener(SWT.Selection, enableListener);
		btnQbqk.addListener(SWT.Selection, enableListener);
		enableListener.handleEvent(null);

		GridData gd_btnNewButton = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_btnNewButton.heightHint = 25;
		gd_btnNewButton.widthHint = 80;
		btnNewButton.setLayoutData(gd_btnNewButton);
		btnNewButton.setBounds(0, 0, 72, 22);
		btnNewButton.setText("确定（F5）");

		Button btnNewButton_1 = new Button(composite_1, SWT.NONE);
		GridData gd_btnNewButton_1 = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_btnNewButton_1.widthHint = 80;
		gd_btnNewButton_1.heightHint = 25;
		btnNewButton_1.setLayoutData(gd_btnNewButton_1);
		btnNewButton_1.setBounds(0, 0, 72, 22);
		btnNewButton_1.setText("取消（ESC）");
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				result = SWT.CANCEL;
				shell.close();
			}
		});
	}

	protected void doOkPressed() {
		if (btnBfqk.getSelection()) {
			if (new BigDecimal(text.getText()).compareTo(new BigDecimal(xfje)) == 1) {
				MessageDialog.openInformation(shell, "提示", "付款金额不能大于消费金额");
				return;
			}
			this.qkje = new BigDecimal(xfje).subtract(
					new BigDecimal(text.getText())).toString();
			if (btnXj.getSelection()) {
				xj = text.getText();
			} else if (btnCzk.getSelection()) {
				czk = text.getText();
			} else if (btnDjq.getSelection()) {
				djq = text.getText();
			} else if (btnXyk.getSelection()) {
				xyk = text.getText();
			}
		} else {
			this.qkje = xfje;
		}
		result = SWT.OK;
		shell.close();
	}

	protected void countQkje() {
		BigDecimal qkje = new BigDecimal(xfje).subtract(new BigDecimal(text
				.getText()));
		labQkje.setText(qkje.toString());
	}

	public static void main(String[] args) {
		QkfkDialog dlg = new QkfkDialog(new Shell(), "650");
		dlg.open();
	}
}
